package com.pb.infra.auth.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.auth.entity.pbsys.MetaPriviPasswdPolicy;
import com.pb.infra.oth.exception.AppAuthException;
import com.pb.infra.oth.exception.ErrorCode;
import com.pb.infra.auth.mapper.custom.MetaPriviPasswdPolicyCustomMapper;
import com.pb.infra.auth.mapper.pbsys.MetaPriviPasswdPolicyMapper;
import com.pb.infra.oth.util.Constant;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统密码策略
 *
 * @Author:liudebao
 * @Date: 2020/12/23
 */
@Service
public class MetaPriviPasswdPolicyService {

    /**
     * 系统密码策略单表DAO映射
     */
    @Resource
    private MetaPriviPasswdPolicyMapper metaPriviPasswdPolicyMapper;

    /**
     * 系统密码策略多表DAO映射
     */
    @Resource
    private MetaPriviPasswdPolicyCustomMapper metaPriviPasswdPolicyCustomMapper;

    /**
     * 查询密码策略
     *
     * @param map
     * @return
     */
    public Result select(Map<String, Object> map, Page page) {
        if (page != null && page.getPageIndex() != null && page.getPageSize() != null) {
            PageHelper.startPage(page.getPageIndex(), page.getPageSize());
        }
        List<Map<String, Object>> list = metaPriviPasswdPolicyCustomMapper.selectData(map);
        return Result.ok(new PageInfo(list).getTotal(), list);
    }

    /**
     * 查询密码规则
     *
     * @param
     * @return
     */
    public Result selectRule() {
        return Result.ok(metaPriviPasswdPolicyCustomMapper.selectRule());
    }

    /**
     * 保存系统密码策略
     *
     * @param metaPriviPasswdPolicy 请求参数
     * @return Map<String, Object> 操作返回结果
     */
    @Transactional
    public Map<String, Object> insert(MetaPriviPasswdPolicy metaPriviPasswdPolicy) {
        Map<String, Object> map2 = new HashMap<>();
        try {
            //校验编号重复
            MetaPriviPasswdPolicy list = metaPriviPasswdPolicyMapper.selectByPrimaryKey(metaPriviPasswdPolicy.getPasswdPolicyId());
            if (list != null) {
                throw new AppAuthException(ErrorCode.ErrorCodeEnum.ACCESS_TABLE_KEY);
            }
            metaPriviPasswdPolicyMapper.insertSelective(metaPriviPasswdPolicy);
            map2.put("code", Constant.OK);
            map2.put("msg", Constant.INSERT_OK_MSG);
        } catch (AppAuthException e) {
            map2.put("msg", e.getMessage());
            map2.put("code", e.getCode());
        }
        return map2;
    }

    /**
     * 修改系统密码策略
     *
     * @param metaPriviPasswdPolicy 请求参数
     * @return Map<String, Object> 操作返回结果
     */
    @Transactional
    public void update(MetaPriviPasswdPolicy metaPriviPasswdPolicy) {
        metaPriviPasswdPolicyMapper.updateByPrimaryKeySelective(metaPriviPasswdPolicy);
    }

    /**
     * 删除系统密码策略
     *
     * @param map 请求参数
     * @return Map<String, Object> 操作返回结果
     */
    @Transactional
    public void delete(List<String> map) {
        // 删除参数
        for (String appId : map) {
            metaPriviPasswdPolicyMapper.deleteByPrimaryKey(appId);
        }
    }

}
